package com.hotel.controller;

import com.hotel.pojo.User;
import com.hotel.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author shkstart  登录控制器
 * @create 2021-06-25 20:33
 */
@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    //前往后台界面
    @GetMapping("/toAdmin")
    public String toAdmin() {
        return "admin/System_index";
    }

    //后台登录
    @PostMapping("/admin/adminLogin")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, User user, Model model, HttpSession session) {
        User byUsername = userService.findByUsername(username);
        if(byUsername != null && byUsername.getPassword().equals(password)){
            session.setAttribute("loginUser",username);
            System.out.println("进来了");
            return "admin/System_dashboard";
        }else {
            //告诉用户，你登录失败了
            model.addAttribute("msg1", "用户名或密码错误!");
            return "admin/System_index";
        }
    }
}
